var snakeCanvas = document.getElementById("snakeCanvas");
var context = snakeCanvas.getContext("2d");
var lock = false; //锁是打开状态
var timer = null;
//创建管理对象
var manager = new Manager();
manager.drawBackgroundLine(20,snakeCanvas,context);

var aSnake = new Snake(4,"red",manager.boxSize);
aSnake.initSnake(context);

var afood = new Food(manager.boxSize,manager.numOfBox);
afood.randomPos();
afood.drawFood(context);


timer = setInterval(function(){
		lock = false;  //开锁
		context.clearRect(0,0,snakeCanvas.width,snakeCanvas.height);
		manager.drawBackgroundLine(20,snakeCanvas,context);
		aSnake.moveSnake();
		aSnake.drawSnakeBody(context);
		afood.drawFood(context);
		//判断蛇是否吃到了食物
		manager.snakeEatFood(aSnake,afood);
		aSnake.crashCheck(snakeCanvas,timer);
},100);


document.onkeydown = function(event){
	var ev = event || window.event;
	switch (ev.keyCode){
		case 37:
		if ((aSnake.direction == "top" || aSnake.direction == "bottom")&& lock == false) {
			aSnake.direction = "left";
			lock = true;
		}
			break;
		case 38:
		if ((aSnake.direction == "left" || aSnake.direction == "right")&& lock == false) {
			aSnake.direction = "top";
			lock = true;
		}
			break;
		case 39:
		if ((aSnake.direction == "top" || aSnake.direction == "bottom")&& lock == false) {
			aSnake.direction = "right";
			lock = true;
		}
			break;1`	
		case 40:
		if ((aSnake.direction == "left" || aSnake.direction == "right")&& lock == false) {
			aSnake.direction = "bottom";
			lock = true;
		}
			break;
		default:
			break;
	}
}
